class Solution {
public:
    vector<vector<string>>ans;
    vector<string>temp;
    vector<vector<bool>>f;
    int n;
    void dfs(string s, int x)
    {
        if (x == n) {
            ans.push_back(temp);
            return;
        }
        for (int i = x; i < n; i++) {
            if (f[x][i]) {
                temp.push_back(s.substr(x, i - x + 1));
                dfs(s, i + 1);
                temp.pop_back();
            }
        }
    }
    vector<vector<string>> partition(string s) {
        n = s.size();
        f.assign(n, vector<bool>(n, 1));
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                f[i][j] = (s[i] == s[j]) && f[i + 1][j - 1];
            }
        }
        dfs(s, 0);
        return ans;
    }
};